package ru.pb.auth.network.engine.auth.packets.receivable;

import java.net.InetAddress;
import java.net.UnknownHostException;

import ru.pb.auth.controllers.AuthController;
import ru.pb.auth.controllers.AuthController.State;
import ru.pb.auth.model.enums.LoginAccess;
import ru.pb.auth.network.clients.AuthClient;
import ru.pb.auth.network.engine.auth.packets.FromAuthClientToAuthServerPacket;
import ru.pb.auth.network.engine.auth.packets.sendable.PROTOCOL_BASE_LOGIN_ACK;
import ru.pb.auth.network.engine.auth.packets.sendable.opcode_545;
import ru.pb.global.configs.DevelopConfig;
import ru.pb.global.network.MacAddr;

/**
 * Запрос на вход на Аутх Сервер
 * 
 * @author Felixx
 */
public class PROTOCOL_BASE_LOGIN_REQ extends FromAuthClientToAuthServerPacket
{
	private String _login, _passwd;
	private int _GAME_VER, _UNK_C2, _UNK_C3, _UNK_C4, _GAME_VER_MONTH, _GAME_VER_DAY, _H;
	private long _UNK_Q2, _UNKH;
	private byte[] bytes, _MAC;
	private InetAddress _ip;

	public PROTOCOL_BASE_LOGIN_REQ()
	{}

	@Override
	public void readImpl()
	{
		_H = readH();
		_GAME_VER = readC();
		readD();
		readC();
		_UNK_C2 = readC();
		_UNK_C3 = readC();
		_login = readS(_UNK_C2);
		_passwd = readS(_UNK_C3);
		_MAC = readB(6);
		_UNKH = readH();
		_UNK_C4 = readC();
		try
		{
			_ip = InetAddress.getByAddress(readB(4));
		}
		catch(UnknownHostException e)
		{
			e.printStackTrace();
		}
		_GAME_VER_MONTH = readH(); // Меняютса в зависимости от версии клиента
		_GAME_VER_DAY = readH(); // Меняютса в зависимости от версии клиента
		_UNK_Q2 = readQ();
		bytes = readB(32);
	}

	@Override
	public void runImpl()
	{
		/*
		 * _log.info("Frost: " + _H); //
		 * _log.info("GAME_VER: " + _GAME_VER); //
		 * _log.info("UNK_C2: " + _UNK_C2);
		 * _log.info("UNK_C3: " + _UNK_C3);
		 * _log.info("login: " + _login); // логин
		 * _log.info("passwd: " + _passwd); // пароль
		 * _log.info("MAC: " + Arrays.toString(_MAC)); // / Хз тут всегда нули..
		 * _log.info("UNKH: " + _UNKH); // какой то 1 байт
		 * _log.info("UNK_C4: " + _UNK_C4); // какой то 1 байт
		 * _log.info("IP: " + _ip.getHostAddress()); // IP сетевухи клиента
		 * _log.info("GAME_DATE: " + _GAME_VER_MONTH + "/" + _GAME_VER_DAY);
		 * _log.info("UNK_Q2: " + _UNK_Q2);
		 * _log.info("UNK_Q2: " + Arrays.toString(bytes));
		 */

		 _log.info("login: " + _login + ";"); // логин
		 _log.info("passwd: " + _passwd + ";"); // пароль
		
		
		// _log.info("Client version: " + _GAME_VER);
		// _log.info("Aviable versions: " + Arrays.toString(DevelopConfig.CLIENT_VERSION.getVersions()));

		AuthClient client = getClient();
		//if( !DevelopConfig.CLIENT_VERSION.isInRange(_GAME_VER))
		//{
		//	client.sendPacket(new PROTOCOL_BASE_LOGIN_ACK(LoginAccess.EVENT_ERROR_EVENT_LOG_IN_TIME_OUT2.get()));
		//	_client.close(false);
		//	return;
		//}
		MacAddr mac = new MacAddr(_MAC);
		_log.info("MAC: " + mac.toString());
		client.setMAC(mac);
		AuthController lc = AuthController.getInstance();

		State status = lc.tryAuthLogin(_login, _passwd, getClient());

		switch(status)
		{
			case VALID:
			{
				client.setAccount(_login);
				// client.setState(ConnectionState.AUTHED);
				client.sendPacket(new PROTOCOL_BASE_LOGIN_ACK(LoginAccess.EVENT_ERROR_SUCCESS.get()));
				client.sendPacket(new opcode_545());
				break;
			}
			case WRONG:
			{
				client.sendPacket(new PROTOCOL_BASE_LOGIN_ACK(LoginAccess.EVENT_ERROR_EVENT_LOG_IN_INVALID_ACCOUNT.get()));
				_client.close(false);
				break;
			}
			case BANNED:
			{
				client.sendPacket(new PROTOCOL_BASE_LOGIN_ACK(LoginAccess.EVENT_ERROR_EVENT_LOG_IN_BLOCK_ACCOUNT.get()));
				_client.close(false);
				break;
			}
			case IP_ACCESS_DENIED:
			{
				client.sendPacket(new PROTOCOL_BASE_LOGIN_ACK(LoginAccess.EVENT_ERROR_EVENT_LOG_IN_TIME_OUT2.get()));
				_client.close(false);
				break;
			}
			case NOT_PAID:
			{
				client.sendPacket(new PROTOCOL_BASE_LOGIN_ACK(LoginAccess.EVENT_ERROR_LOGIN.get()));
				_client.close(false);
				break;
			}
			case IN_USE:
			{
				client.sendPacket(new PROTOCOL_BASE_LOGIN_ACK(LoginAccess.EVENT_ERROR_EVENT_LOG_IN_ALEADY_LOGIN.get()));
				_client.close(false);
				break;
			}
			default:
			{
				_client.close(false);
				return;
			}
		}
	}
}